package com.idp.web.test.controller;

import com.idp.web.test.entity.ProductType;
import com.idp.web.test.service.ProductTypeService;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.idp.common.base.BaseController;
import com.idp.common.persistence.Page;
import com.idp.common.util.ValidateUtils;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

/**
 * 
 * 产品类型controller
 * 
 * <pre>
 * 	历史记录：
 * 	2017-06-02 Amber
 * 	新建文件
 * </pre>
 * 
 * @author
 * 
 *         <pre>
 * SD
 * 	Amber
 * PG
 *	Amber
 * UT
 *
 * MA
 *         </pre>
 * 
 * @version $Rev$
 *
 *          <p/>
 *          $Id$
 *
 */
@Controller
@RequestMapping("/productType")
public class ProductTypeController extends BaseController {

	private Logger logger = Logger.getLogger(ProductTypeController.class);

	@Resource
	private ProductTypeService productTypeService;

	/**
	 * 
	 * <pre>
	 * 	2017-06-02 Amber
	 * 	初始化查询页面
	 * </pre>
	 * 
	 * @return
	 */
	@RequestMapping("/init")
	public String init(HttpServletRequest request) {

		return "shopping/productType/productTypeSearch";
	}

	/**
	 * 
	 * <pre>
	 * 	2017-06-02 Amber
	 * 	分页查询
	 * </pre>
	 * 
	 * @param productType
	 * @param page
	 * @param request
	 * @return
	 */
	@RequestMapping("/list")
	public String list(ProductType productType, Page<ProductType> page, HttpServletRequest request) {

		try {
//			request.setAttribute("productTypes", productTypeService.findForTreeTable(0));
			request.setAttribute("productTypes", productTypeService.findForTreeTableNew());
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
		}

		return "shopping/productType/productTypeList";
	}

	/**
	 * 
	 * <pre>
	 * 	2017-06-02 Amber
	 * 	新增修改页面初始化
	 * </pre>
	 * 
	 * @param id
	 * @param request
	 * @return
	 */
	@RequestMapping("/productType")
	public String productType(Integer id, Integer parent,HttpServletRequest request) {
		try {
			ProductType productType = new ProductType();
			if (ValidateUtils.isNotEmpty(id)) {
				productType = productTypeService.getById(id);
			}
			if (ValidateUtils.isNotEmpty(parent)) {
				productType.setParent(parent);
			}
			request.setAttribute("productType", productType);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
		}
		return "shopping/productType/productType";
	}

	@RequestMapping("/treeTable")
	@ResponseBody
	public String treeTable(Integer parentId) {
		JSONArray json = new JSONArray();

		try {
			json.addAll(productTypeService.findForTreeTable(parentId));
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
		}
		return json.toString();
	}

	/**
	 * 
	 * <pre>
	 * 	2017-06-02 Amber
	 * 	保存
	 * </pre>
	 * 
	 * @param productType
	 * @return
	 */
	@RequestMapping("/save")
	@ResponseBody
	public String save(ProductType productType) {
		JSONObject json = new JSONObject();
		try {
			productType.setCode(productType.getName());
			// 修改
			if (ValidateUtils.isNotEmpty(productType.getId())) {
				productTypeService.update(productType);
			} else {// 新增
				productTypeService.add(productType);
			}
			json.put("result", "save_success");
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			json.put("result", "save_fail");
		}
		return json.toString();
	}

	/**
	 * 
	 * <pre>
	 * 	2017-06-02 Amber
	 * 	删除
	 * </pre>
	 * 
	 * @param id
	 * @return
	 */
	@RequestMapping("/delete")
	@ResponseBody
	public String delete(Integer id) {
		JSONObject json = new JSONObject();
		try {
			productTypeService.delete(id);
			json.put("result", "delete_success");
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			json.put("result", "delete_fail");
		}
		return json.toString();
	}
	
	/**
	 * 获得类型树
	 * 
	 * @return
	 */
	@RequestMapping("/getTypeTree")
	@ResponseBody
	public String getTypeForTree() {
		JSONArray jsonArray = new JSONArray();
		List<ProductType> productTypes = productTypeService.findBySearch(null);
		if (productTypes != null && productTypes.size() != 0) {
			for (ProductType productType : productTypes) {
				Map<String,Object> mapTree = new HashMap<>();
				mapTree.put("id", productType.getId());
				mapTree.put("name", productType.getName());
				mapTree.put("pId", productType.getParent());
				jsonArray.add(mapTree);
			}
		}
		return jsonArray.toString();
	}
}
